package com.bianmaba.spring.security.autoconfig;

import com.bianmaba.spring.security.basic.configuration.BasicWebSecurityConfiguration;
import com.bianmaba.spring.security.basic.configuration.BasicWebSecurityConfigurerAdapter;
import com.bianmaba.spring.security.basic.configuration.SecurityPropertiesConfiguration;
import com.bianmaba.spring.security.database.configuration.DatabaseWebSecurityConfigurerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import static com.bianmaba.spring.security.autoconfig.WebSecurityAutoConfiguration.ORDER;


/**
 * spring-security自动化配置类
 * Created by cwx183898 on 2018/4/20.
 */
@Configuration
//此类由spring.factories加载，如果此类加载前已经存在实例，则此类将不会被实例化，但此类的注解将被继承
@ConditionalOnClass({WebSecurityConfigurerAdapter.class})
//如果此类加载前已经存在指定类型的实例，则此类将不会被实例化(如果有继承关系，则可能会执行构造函数，但实际为同一个实例时为正常，否则可能不正常)
@ConditionalOnMissingBean({WebSecurityAutoConfiguration.class})
@ConditionalOnWebApplication(
        type = ConditionalOnWebApplication.Type.SERVLET
)
//实例化并引用目标类,这里引用两个Adapter,实际化哪一个由spring.security.type决定
@Import({SecurityPropertiesConfiguration.class, BasicWebSecurityConfiguration.class, DatabaseWebSecurityConfigurerAdapter.class, BasicWebSecurityConfigurerAdapter.class})
//配置初始化排序，此值越低，越靠前，但@Import引入的类此注解无效
@Order(ORDER)
public class WebSecurityAutoConfiguration {
    private static final Logger LOG = LoggerFactory.getLogger(WebSecurityAutoConfiguration.class);
    public static final int ORDER = 2147483641;

    public WebSecurityAutoConfiguration() {
        if (this.getClass().getName().equals(WebSecurityAutoConfiguration.class.getName())) ;
        {
            LOG.info("加载WEB安全性化配置(order=" + ORDER + "):" + this.getClass().getName());
        }
    }
}
